package com.javaenzk.wfx.manager.service;

import com.javaenzk.wfx.manager.dao.CustomerDAO;
import com.javaenzk.wfx.manager.dao.TbImagesDAO;
import com.javaenzk.wfx.manager.dao.WxbGoodDAO;
import com.javaenzk.wfx.manager.po.Wxb_Customer;
import com.javaenzk.wfx.manager.po.Wxb_Good;
import com.javaenzk.wfx.manager.po.Wxb_Good_Sku;
import com.javaenzk.wfx.manager.po.Wxb_Good_Type;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class WxbGoodService implements IWxbGoodService{

    @Autowired
    private WxbGoodDAO wxbGoodDAO;

    @Autowired
    private CustomerDAO customerDAO;

    @Autowired
    private TbImagesDAO imagesDAO;


    public List<Wxb_Good_Type> queryGoodtype() throws  Exception{

        return wxbGoodDAO.queryAllType();
    }

    @Transactional
    public  void  saveWxbGood(Wxb_Good wxb_good) throws  Exception{

        Wxb_Customer wxb_customer = (Wxb_Customer)SecurityUtils.getSubject().getSession().getAttribute("user");

        String ids = UUID.randomUUID().toString();

        wxb_good.setGood_Id(ids);
        wxb_good.setCustomer_Id(wxb_customer.getCustomer_Id());
        wxb_good.setCreate_Time(new Timestamp(new Date().getTime()));

        wxbGoodDAO.saveWxbGood(wxb_good);



        //完成套餐ID的生成和配置、商品Id设置到套餐中
        List<Wxb_Good_Sku> skuList = wxb_good.getListsku();
        int size = skuList.size();
        for (int i = 0; i < size; i++) {
            Wxb_Good_Sku wxbGoodSku = skuList.get(i);
            //套餐ID的配置
            wxbGoodSku.setSku_Id(UUID.randomUUID().toString());
            wxbGoodSku.setGood_Id(ids);
        }

        //修改图片的type类型为已使用
        imagesDAO.uodateImages(wxb_good.getGood_Pic(),"1","1");
        imagesDAO.uodateImages(wxb_good.getGood_Pic1(),"1","1");
        imagesDAO.uodateImages(wxb_good.getGood_Pic2(),"1","1");

        saveGoodsSku(skuList);
    }

    public List<Wxb_Good> queryAllwxbGoods() throws Exception{

        Wxb_Customer wxb_customer = (Wxb_Customer)SecurityUtils.getSubject().getSession().getAttribute("user");


        return wxbGoodDAO.queryAllwxbGoods(wxb_customer.getCustomer_Id());
    }


    public Wxb_Customer queryBynameAndPwd(String usename,String pwd) throws AuthenticationException{

        if (usename == null || usename == "")
        {
            throw  new  NullPointerException("username is null");
        }

        if (pwd == null || pwd == "")
        {
            throw  new  NullPointerException("pwd is null");
        }

        final Wxb_Customer wxbCustomer = customerDAO.queryCustomerByName(usename);

        if (wxbCustomer == null)
        {
            throw  new NullPointerException("user is not exist");
        }

        if (!pwd.equals(wxbCustomer.getLogin_Pwd()))
        {
            throw  new  NullPointerException("password is error");
        }

        return wxbCustomer;

    }

    @Override
    public void saveGoodsSku(List<Wxb_Good_Sku> skuList) {

        wxbGoodDAO.saveGoodsSku(skuList);

    }

}
